package com.training1.core.web.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.training1.core.common.controller.BaseController;
import com.training1.core.common.dao.Page;
import com.training1.core.common.entity.UserEntity;
import com.training1.core.common.enums.DropDownDto;
import com.training1.core.common.global.signIn.LoginInfoUtil;
import com.training1.core.common.utils.StringUtils;
import com.training1.core.entity.MemberEntity;
import com.training1.core.entity.PaymentEntity;
import com.training1.core.entity.SystemUserEntity;
import com.training1.core.modules.service.PaymentService;

@Controller
@RequestMapping("payment")
public class PaymentController extends BaseController{
	
	@Resource
	private PaymentService paymentService;
	
	
	/**
	 * 进入页面
	 * @return
	 */
	@RequestMapping(value={"","/"})
	public String go(){
		return "payment/payment";
	}
	
	/**
	 * 获取开单中心的下拉框
	 * @return
	 */
	@RequestMapping("getCenter")
	@ResponseBody
	public List <SystemUserEntity>  getCenter(){
		List<SystemUserEntity> userItems = new ArrayList<SystemUserEntity>();
		Map<String, Object> param = new HashMap<>();
		param.put("userRole", 1);
		List<SystemUserEntity> items = paymentService.findCenter(param);
		userItems.addAll(items);
		
		return userItems;
	}
	
	/**
	 * 查询
	 * @param startTime
	 * @param endTime
	 * @param center
	 * @param cellPhone
	 * @param memberName
	 * @param paymentType
	 * @param page
	 * @return
	 */
	
	@RequestMapping(value="list",method=RequestMethod.GET)
	@ResponseBody
	public Page<PaymentEntity> list(String startTime,String endTime,String center,String cellPhone,String memberName, String paymentType,Page<PaymentEntity> page){
		page.getSearch().put("startTime", startTime);
		page.getSearch().put("endTime", endTime);
		page.getSearch().put("memberName",memberName);
		page.getSearch().put("center", center);
		page.getSearch().put("cellPhone", cellPhone);
		page.getSearch().put("paymentType",paymentType);
		
		paymentService.findList(page.getSearch());
		
		/**
		 * 封装前台footer  显示总金额
		 */
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("startTime", startTime);
		param.put("endTime", endTime);
		param.put("center", center);
		param.put("cellPhone",cellPhone);
		param.put("memberName",memberName);
		param.put("paymentType",paymentType);
		
		PaymentEntity payment = paymentService.findAllCash(param);//查询出总金额
		//如果沒有数据
		if(null == payment){
			payment = new PaymentEntity();
			payment.setCash(0.0);
		} 
		
		payment.setCellPhone("总合计：");
		List<PaymentEntity> cashes = new ArrayList<PaymentEntity>();
		
		cashes.add(payment);
		page.setFooter(cashes);
		return page;
	}
	
	/**
	 * 导出excel
	 * @param resp
	 * @param blackListFlag
	 * @param cellphone
	 * @param memberName
	 * @param branchId
	 * @param userTrust
	 */
	@RequestMapping("export")
	@ResponseBody
	public void export(HttpServletResponse resp,String name, String startTime,String endTime,String cellPhone,String memberName,String paymentType){
		//设置参数
	Map<String, Object> param = new HashMap<String, Object>();
	param.put("name", name);
	param.put("startTime", startTime);
	param.put("endTime", endTime);
	param.put("cellPhone",cellPhone);
	param.put("memberName",memberName);
	param.put("paymentType",paymentType);
	
	paymentService.export(resp, param);
}
	
	/**
	 * 查询生成报表
	 */
	@RequestMapping("getReport")
	@ResponseBody
	public List <PaymentEntity>  getReport(){
		List<PaymentEntity> reports = paymentService.findReport();
		return reports;
	}
	
}
